home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Printing / PrintWithMargins / PrintWithMargins.cs next >
Encoding:
Text File  |  2001-01-15  |  2.4 KB  |  71 lines

  1. //-----------------------------------------------
  2. // PrintWithMargins.cs ⌐ 2001 by Charles Petzold
  3. //-----------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Printing;
  7. using System.Windows.Forms;
  8.  
  9. class PrintWithMargins: Form
  10. {
  11.      public static void Main()
  12.      {
  13.           Application.Run(new PrintWithMargins());
  14.      }
  15.      public PrintWithMargins()
  16.      {
  17.           Text = "Print with Margins";
  18.  
  19.           Menu = new MainMenu();
  20.           Menu.MenuItems.Add("&File");
  21.           Menu.MenuItems[0].MenuItems.Add("&Print...", 
  22.                                    new EventHandler(MenuFilePrintOnClick));
  23.      }
  24.      void MenuFilePrintOnClick(object obj, EventArgs ea)
  25.      {
  26.                // Create PrintDocument.
  27.  
  28.           PrintDocument prndoc = new PrintDocument();
  29.  
  30.                // Create dialog box and set PrinterName property.
  31.  
  32.           PrinterSelectionDialog dlg = new PrinterSelectionDialog();
  33.           dlg.PrinterName = prndoc.PrinterSettings.PrinterName;
  34.  
  35.                // Show dialog box and bail out if not OK.
  36.  
  37.           if (dlg.ShowDialog() != DialogResult.OK)
  38.                return;
  39.  
  40.                // Set PrintDocument to selected printer.
  41.  
  42.           prndoc.PrinterSettings.PrinterName = dlg.PrinterName;
  43.  
  44.                // Set remainder of PrintDocument properties and commence.
  45.  
  46.           prndoc.DocumentName = Text;
  47.           prndoc.PrintPage += new PrintPageEventHandler(OnPrintPage);
  48.           prndoc.Print();
  49.      }
  50.      void OnPrintPage(object obj, PrintPageEventArgs ppea)
  51.      {
  52.           Graphics   grfx  = ppea.Graphics;
  53.           RectangleF rectf = new RectangleF(
  54.                ppea.MarginBounds.Left - 
  55.                (ppea.PageBounds.Width - grfx.VisibleClipBounds.Width) / 2,
  56.                ppea.MarginBounds.Top - 
  57.                (ppea.PageBounds.Height - grfx.VisibleClipBounds.Height) / 2,
  58.                ppea.MarginBounds.Width,
  59.                ppea.MarginBounds.Height);
  60.  
  61.           grfx.DrawRectangle(Pens.Black, rectf.X, rectf.Y, 
  62.                                          rectf.Width, rectf.Height);
  63.  
  64.           grfx.DrawLine(Pens.Black, rectf.Left, rectf.Top, 
  65.                                     rectf.Right, rectf.Bottom);
  66.  
  67.           grfx.DrawLine(Pens.Black, rectf.Right, rectf.Top, 
  68.                                     rectf.Left, rectf.Bottom);
  69.      }
  70. }
  71.